/*
 * Copyright (c) 2015 Minshenglife. All rights reserved. Minshenglife
 * PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.sentry.commons.utils;

import org.joda.time.DateTimeFieldType;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;

import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理工具，基于jodatime实现
 * 
 * @author WuBo (wub1@minshenglife.com)
 *
 */
public class Datetimes {

    private static final String DEFAULT_TIME_ZONE = "Asia/Shanghai";
    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    private static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
    private static final String DEFAULT_DATE_TIME_FORMAT = DEFAULT_DATE_FORMAT
            + " " + DEFAULT_TIME_FORMAT;


    private Datetimes() {}

    /**
     * 获取日期中年份
     * 
     * @param date
     * @return
     */
    public static String getYear(final Date date) {
        return get(date, DateTimeFieldType.year());
    }

    /** 获取日期中的月份 */
    public static String getMonth(final Date date) {
        return get(date, DateTimeFieldType.monthOfYear());
    }

    /** 获取日期的天 */
    public static String getDay(final Date date) {
        return get(date, DateTimeFieldType.dayOfMonth());
    }

    /**
     * 获取Date对象字段值
     * 
     * @param date
     * @param field
     * @return
     */
    public static String get(final Date date, final DateTimeFieldType field) {
        return String.valueOf(LocalDateTime.fromDateFields(date).get(field));
    }


    public static Date parseDate(final String str, final String format) {
        return LocalDateTime.parse(str, DateTimeFormat.forPattern(format))
                .toDate();
    }


    /**
     * 根据字符串构建日期(默认格式yyyy-MM-dd)
     * 
     * @param str
     * @return
     */
    public static Date parseDate(String str) {
        return parseDate(str, DEFAULT_DATE_FORMAT);
    }


    /**
     * 根据字符串构建日期(默认格式yyyy-MM-dd HH:mi:ss)
     * 
     * @param str
     * @return
     */
    public static Date parseDateTime(String str) {
        return parseDate(str, DEFAULT_DATE_TIME_FORMAT);
    }


    /**
     * 将日期转换成字符串
     * 
     * @param date
     * @return
     */
    public static String formatDate(final Date date, final String format) {
        return LocalDateTime.fromDateFields(date).toString(format);
    }



    /**
     * 将日期转换成字符串(默认格式yyyy-MM-dd)
     * 
     * @param date
     * @return
     */
    public static String formatDate(final Date date) {
        return formatDate(date, DEFAULT_DATE_FORMAT);
    }

    /**
     * 将日期转换成字符串(默认格式yyyy-MM-dd HH:mi:ss)
     * 
     * @param date
     * @return
     */
    public static String formatDateTime(final Date date) {
        return formatDate(date, DEFAULT_DATE_TIME_FORMAT);
    }



    /**
     * 返回jodatime的实例 ，使用 {@link DEFAULT_TIME_ZONE}作为默认时区
     */
    public static LocalDateTime joda(Date date) {
        return joda(date, DateTimeZone.forID(DEFAULT_TIME_ZONE));
    }

    /**
     * 返回jodatime的实例
     * 
     * @return
     */
    public static LocalDateTime joda(final Date date,
            final DateTimeZone timezone) {
        return new LocalDateTime(date.getTime(), timezone);
    }



    /**
     * 获取所在月的最后一天
     * 
     * @param date
     * @return
     */
    public static int getLastDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getActualMaximum(Calendar.DAY_OF_MONTH);
    }



}
